#include "frmlogon.h"
#include "ui_frmlogon.h"

frmLogon::frmLogon(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::frmLogon)
{
    ui->setupUi(this);
    QImage image1;
    image1.load(":/img/backgroundlo.png");
    QImage image2=image1.scaled(350,350);

    QPalette palette;
    palette.setBrush(this->backgroundRole(),QBrush(image2));//设置背景
    setPalette(palette);
}

frmLogon::~frmLogon()
{
    delete ui;
}

void frmLogon::on_btnClose_clicked()
{
    this->close();
}

void frmLogon::on_btnLogon_clicked()
{
    string name = ui->letName->text().toStdString();
    string password = ui->letPassword->text().toStdString();
    if(judge(name, password)){
        User *u = new User(name, password, "0", "0");
        //用户添加到容器中
        UserList::userlist->push_back(u);
        ofstream ofs("userlist.txt", ios::out | ios::app);
        string data ="";
        data = u->name + " " + u->password + " " + u->number + " " + u->winNumber;
        ofs << data << endl;
        ofs.close();
        QMessageBox::information(this,"成功", "注册成功！");
        this->close();
    }
}

bool frmLogon::judge(string name, string password)
{
    if(name == "" || password == ""){
            QMessageBox::critical(this, "错误", "注册失败！\n可能原因:姓名或密码为空！");
            return false;
        }
        for(int i = 0; i < UserList::userlist->size(); i++){
            User *u = UserList::userlist->at(i);
            if(name == u->name){
                QMessageBox::critical(this, "错误", "注册失败！昵称已被使用！");
                return false;
            }
        }
        return true;
}

